package com.photobucket.webapi.oauth
{

        import flexunit.framework.TestCase;
        import flexunit.framework.TestSuite;
        import flash.net.URLVariables;
        import com.photobucket.webapi.oauth.oauth_internal;
        
        use namespace oauth_internal;

		public class OAuthBaseServiceTest extends TestCase {

			public function OAuthBaseServiceTest(methodName:String = null) {
				super(methodName);
			}

			public function testTimestamp():void {
				var service:OAuthBaseService = new OAuthBaseService();
				var date:Date = new Date();
				var min:Number = date.time * 0.0009;
				var max:Number = date.time * 0.0011;
				var value:Number = Number(service.oauth_timestamp);
				assertTrue("Timestamp in Seconds", ((value > min)&&(value < max)));
			}

			public function testEncodedURL():void {
				var service:OAuthBaseService = new OAuthBaseService();
				var inUrl:String = "http://api.photobucket.com";
				var outUrl:String = service.getEncodedURL(inUrl);
				assertEquals("URL Encoded Properly", "http%3A%2F%2Fapi.photobucket.com", outUrl);
				inUrl = "http://api123.photobucket.com";
				outUrl = service.getEncodedURL(inUrl);
				assertEquals("URL Encoded Properly", "http%3A%2F%2Fapi123.photobucket.com", outUrl);
			}

			public function testBaseString():void {
				var service:OAuthBaseService = new OAuthBaseService();
				var request:IOAuthRequest = new OAuthRequest();
				service.oauth_consumer_key = "consumerkey";
				service.oauth_consumer_secret = "consumersecret";
				service.oauth_token = "oauthtoken";
				service.oauth_token_secret = "oauthtokensecret";
				request.method = "GET";
				request.url = "http://api.photobucket.com/action/ident";
				request.addParameter("param1", "value");
				request.addParameter("param2", "value");
				var baseString:String = service.getBaseString(request);
				assertEquals("Base String Matches Expected", "GET&http%3A%2F%2Fapi.photobucket.com%2Faction%2Fident&param1%3Dvalue%26param2%3Dvalue", baseString);
				assertNotContained("Private Key not in Basestring", "oauthtokensecret", baseString);
			}

			public function testSignature():void {
				var service:OAuthBaseService = new OAuthBaseService();
				service.oauth_consumer_key = "consumerkey";
				service.oauth_consumer_secret = "consumersecret";
				service.oauth_token = "oauthtoken";
				service.oauth_token_secret = "oauthtokensecret";
				var request:IOAuthRequest = new OAuthRequest();
				request.method = "GET";
				request.needsLogin = false;
				request.url = "http://api.photobucket.com/action/ident";
				request.addParameter("param1", "value");
				request.addParameter("param2", "value");
				var sig:String = service.getSignature(request);
				assertEquals("Sig Matchs Not Logged In", "MGbZDiC2tI3+K6CB+NHvJqeZeAQ=", sig);
				request.needsLogin = true;
				sig = service.getSignature(request);
				assertEquals("Sig Matchs Logged In", "1n5VtProYBCyCHt4mJbPhAPxMSQ=", sig);
			}

			public function testSignRequestLoggedOut():void {
				var service:OAuthBaseService = new OAuthBaseService();
				service.oauth_consumer_key = "consumerkey";
				service.oauth_consumer_secret = "consumersecret";
				service.oauth_token = "oauthtoken";
				service.oauth_token_secret = "oauthtokensecret";
				var request:IOAuthRequest = new OAuthRequest();
				request.method = "GET";
				request.needsLogin = false;
				request.url = "http://api.photobucket.com/action/ident";
				request.addParameter("param1", "value");
				request.addParameter("param2", "value");
				service.signRequest(request);
				var paramString:String = request.parameterString;
				assertContained("Consumer added to request", "oauth_consumer_key=consumerkey", paramString);
				assertContained("Timestamp added to request", "oauth_timestamp=", paramString);
				assertContained("Nonce added to request", "oauth_nonce=", paramString);
				assertContained("Version added to request", "oauth_version=1.0", paramString);
				assertContained("Signature method added to request", "oauth_signature_method=HMAC-SHA1", paramString);
				assertNotContained("Token Not Added", "oauth_token", paramString);
				assertNotContained("Secrets not added", "secret", paramString);
			}

			public function testSignRequestLoggedIn():void {
				var service:OAuthBaseService = new OAuthBaseService();
				service.oauth_consumer_key = "consumerkey";
				service.oauth_consumer_secret = "consumersecret";
				service.oauth_token = "oauthtoken";
				service.oauth_token_secret = "oauthtokensecret";
				var request:IOAuthRequest = new OAuthRequest();
				request.method = "GET";
				request.needsLogin = true;
				request.url = "http://api.photobucket.com/action/ident";
				request.addParameter("param1", "value");
				request.addParameter("param2", "value");
				service.signRequest(request);
				var paramString:String = request.parameterString;
				assertContained("Consumer added to request", "oauth_consumer_key=consumerkey", paramString);
				assertContained("Timestamp added to request", "oauth_timestamp=", paramString);
				assertContained("Nonce added to request", "oauth_nonce=", paramString);
				assertContained("Version added to request", "oauth_version=1.0", paramString);
				assertContained("Signature method added to request", "oauth_signature_method=HMAC-SHA1", paramString);
				assertContained("Token Added", "oauth_token=oauthtoken", paramString);
				assertNotContained("Secrets not added", "secret", paramString);
			}

		}
}